﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HttpContextCacheAdapter.cs" company="">
//   
// </copyright>
// <summary>
//   The http context cache adapter.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

#region

using System.Runtime.Caching;
using System.Web;

#endregion

namespace Common.Cache.CacheStorage
{
    /// <summary>
    /// The http context cache adapter.
    /// </summary>
    public class HttpContextCacheAdapter : ICacheStorage
    {
        /// <summary>
        /// The remove.
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        public void Remove(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        /// <summary>
        /// The store.
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        public void Store(string key, object data)
        {
            HttpContext.Current.Cache.Insert(key, data);
        }

        /// <summary>
        /// The retrieve.
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public T Retrieve<T>(string key)
        {
            T itemStored = (T) HttpContext.Current.Cache.Get(key);
            if (itemStored == null)
                itemStored = default(T);

            return itemStored;
        }
    }
}